package io.gitee.kotle.android.mvvm

import android.content.Context
import android.content.res.Resources
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import androidx.activity.ComponentDialog
import androidx.annotation.StyleRes
import io.gitee.kotle.android.R
import io.gitee.kotle.android.delegate.unsafeLazy
import io.gitee.kotle.android.mvvm.mvvminterface.IBaseMvvm
import io.gitee.kotle.android.mvvm.mvvminterface.ScreenOrientationHelper
import io.gitee.kotle.android.utils.asOrNull
import io.gitee.kotle.android.utils.fullScreen
import io.gitee.kotle.android.utils.transparentStatusBar


/**
 *
 * 描述：
 *
 * @author jiale.wei
 *
 * 创建时间：2022/7/14 11:10
 *
 */

abstract class MvvmAlert(context: Context) : ComponentDialog(context, R.style.MvvmDialogStyle),
    IBaseMvvm {
    private var cancelable = true
    private var canceledOnTouchOutside = true
    private val screenOrientationHelper by unsafeLazy { ScreenOrientationHelper(this) }
    private var touchDismissView: DialogRootLayout? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        if (isFullScreen) {
            window.fullScreen(true)
        } else {
            window?.transparentStatusBar(
                isBlackStatusBarTextColor = false,
                isBlackNavigationBarTextColor = false
            )
        }
        super.onCreate(savedInstanceState)
        //region 将view与activity关联
        val root = DialogRootLayout(context)
        touchDismissView = root
        onHandleCreateMvvmViewType(layoutInflater, root, savedInstanceState)?.let {
            onDialogViewCreated(root, it, savedInstanceState)
            val lp = it.layoutParams
            if (lp != null) {
                root.addView(it, lp)
            } else {
                root.addView(it)
            }
            val rootLp = root.layoutParams
            if (rootLp != null) {
                setContentView(root, rootLp)
            } else {
                setContentView(root)
            }
        }
        //endregion
        onViewCreatedFinish(this, savedInstanceState, context.resources)
    }

    override fun setCancelable(flag: Boolean) {
        super.setCancelable(flag)
        touchDismissView?.isCancelable = flag
        cancelable = false
    }

    /**
     * 是否可以取消
     */
    open fun getCancelable(): Boolean = cancelable

    override fun setCanceledOnTouchOutside(cancel: Boolean) {
        super.setCanceledOnTouchOutside(cancel)
        touchDismissView?.isCanceledOnTouchOutside = cancel
        canceledOnTouchOutside = cancel
    }

    /**
     * 是否可以触摸外部取消
     */
    open fun getCanceledOnTouchOutside(): Boolean = canceledOnTouchOutside

    /**
     * 是否是全屏
     */
    open var isFullScreen = false

    final override fun checkScreenPortrait(resources: Resources) {
        screenOrientationHelper.checkScreenPortrait(resources)
    }

    /**
     * view创建完成，可以自定义自己的逻辑
     */
    protected open fun onDialogViewCreated(
        contain: DialogRootLayout,
        view: View,
        savedInstanceState: Bundle?
    ) {
        contain.dialog = this
        contain.dialogView = view
        contain.isCancelable = cancelable
        contain.isCanceledOnTouchOutside = canceledOnTouchOutside
        contain.setBackgroundResource(R.drawable.mvvm_dialog_background)
        //设置默认动画
        getMvvmWindowAnimations(view, view.layoutParams.asOrNull())?.let {
            window?.setWindowAnimations(it)
        }
    }

    /**
     * 设置对话框进入退出动画
     */
    @StyleRes
    protected open fun getMvvmWindowAnimations(
        view: View,
        layoutParams: FrameLayout.LayoutParams?
    ): Int? {
        return when (layoutParams?.gravity) {
            Gravity.BOTTOM -> {
                R.style.PopupWindowBottomIn
            }
            Gravity.TOP -> {
                R.style.PopupWindowTopIn
            }
            else -> {
                R.style.AnimScaleCenter
            }
        }
    }
}